package com.dhcc.sds.standard.controller;


import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dhcc.sds.standard.entity.SdsStandDicEntity;
import com.dhcc.sds.standard.entity.SdsStandDicsubEntity;
import com.dhcc.sds.standard.service.ISdsStandDicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;



import com.dhcc.sds.standard.entity.SdsStandBaseMatchEntity;
import com.dhcc.sds.standard.service.ISdsStandBaseMatchService;


/**
 * 标准化_基础信息关键字
 * 
 * @ClassName: SdsStandBaseMatchController
 * @Description:
 * @author sunyingjie
 * @date 2021-03-22 10:09:56
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsstandbasematch")
public class SdsStandBaseMatchController extends BaseController {

	private String PREFIX = "/backend/sds/sdsstandbasematch/";

	@Autowired
	private ISdsStandBaseMatchService sdsStandBaseMatchService;

    @Autowired
    private ISdsStandDicService sdsStandDicService;
	

    /**
     * 跳转到标准化_基础信息关键字列表首页
     */
    @RequestMapping("")
    public String index(Model model) {
        /*model.addAttribute("dicKey","");
        model.addAttribute("dicFilter","");*/
        model.addAttribute("dicMap",sdsStandDicService.getDicMapBySource("常用短语"));
        return PREFIX + "sdsstandbasematch.html";
    }
	
    /**
     * 跳转到新增标准化_基础信息关键字
     */
    @RequestMapping("/to_add")
    public String toAdd() {
        return PREFIX + "sdsstandbasematch_add.html";
    }
	
	/**
     * 跳转到修改标准化_基础信息关键字
     */
    @RequestMapping("/to_update/{sdsStandBaseMatchId}")
    public String toUpdate(@PathVariable Long sdsStandBaseMatchId, Model model) {
        SdsStandBaseMatchEntity sdsStandBaseMatch = this.sdsStandBaseMatchService.selectById(sdsStandBaseMatchId);
        model.addAttribute("sdsStandBaseMatch",sdsStandBaseMatch);
        return PREFIX + "sdsstandbasematch_edit.html";
    }

    /**
     * 获取标准化_基础信息关键字列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<SdsStandBaseMatchEntity> page = new PageFactory<SdsStandBaseMatchEntity>().defaultPage();
        List<SdsStandBaseMatchEntity> result = this.sdsStandBaseMatchService.page(page,params);
        page.setRecords((List<SdsStandBaseMatchEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增标准化_基础信息关键字
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SdsStandBaseMatchEntity sdsStandBaseMatch) {
        if (CommonUtil.isOneEmpty(sdsStandBaseMatch)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsStandBaseMatch.insert();
        return R.ok();
    }

    /**
     * 删除标准化_基础信息关键字
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long sdsStandBaseMatchId) {
	
        this.sdsStandBaseMatchService.deleteById(sdsStandBaseMatchId);

        return R.ok();
    }

    /**
     * 修改标准化_基础信息关键字
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SdsStandBaseMatchEntity sdsStandBaseMatch) {
        if (CommonUtil.isOneEmpty(sdsStandBaseMatch)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsStandBaseMatchEntity old = this.sdsStandBaseMatchService.selectById(sdsStandBaseMatch.getId());

        old.setDicsubId(sdsStandBaseMatch.getDicsubId());
        old.setResultKeys(sdsStandBaseMatch.getResultKeys());
        old.setResultFilter(sdsStandBaseMatch.getResultFilter());
        old.updateAllColumnById();
        return R.ok();
    }

    /**
     * 根据字典子项id查找基础信息关键字
     */
    @RequestMapping(value = "/getByDicSubId")
    @ResponseBody
    public Object getByDicSubId(@RequestParam Map<String, Object> params) {
        /*model.addAttribute("dicKey",this.sdsStandBaseMatchService.getDicKeyByDicSubId(dicSubId));
        model.addAttribute("dicFilter",this.sdsStandBaseMatchService.getDicFilterByDicSubId(dicSubId));
        return R.ok();*/
        Wrapper<SdsStandBaseMatchEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("dicsub_id", params.get("dicSubId"));
        return this.sdsStandBaseMatchService.selectOne(wrapper);
    }


    /**
     * 保存标准化_基础信息关键字
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Object save(SdsStandBaseMatchEntity sdsStandBaseMatch) {
        /*SdsStandBaseMatchEntity sdsStandBaseMatch = new SdsStandBaseMatchEntity();
        sdsStandBaseMatch.setDicSubDr(dicSubId);
        sdsStandBaseMatch.setResultKeys(dicKey);
        sdsStandBaseMatch.setResultFilter(dicFilter);
        sdsStandBaseMatch.insert();
        return R.ok();*/
        if (CommonUtil.isOneEmpty(sdsStandBaseMatch)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsStandBaseMatch.insertOrUpdate();
        return R.ok();
    }

    /**
     * 对照的标准字典进行渲染
     */
    @RequestMapping(value = "/matchDicsub")
    @ResponseBody
    public Object matchDicsub() {

        List<Long> matchIds = this.sdsStandBaseMatchService.selectMatchDicsub();

        return matchIds;
    }
	
}
